//
//  CMDBSliderMenuController.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/13.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let kSliderMenuDuraton : NSTimeInterval = 0.2

private let ratio : CGFloat = 0.86

class CMDBSliderMenuController: CMDBHomeBaseViewController {
    
    var mainVC : CMDBTabBarController?

    lazy var navLeft : CustomNavigationControllerViewController = {
        let navLeft = CustomNavigationControllerViewController(rootViewController: self.leftVc! , hideNavBar: true)
        
        return navLeft
    }()
    
    lazy var leftVc : CMDBHomeLeftViewController? = {
        
        let leftVc = CMDBHomeLeftViewController()
        
        leftVc.sliderVc = self
        
        return leftVc
    }()
    
    var leftCenterPoint : CGPoint?
    
    var thisWindow : UIWindow?
    override func setup() {
        super.setup()
        
        self.mainVC = UIApplication.sharedApplication().keyWindow!.rootViewController as? CMDBTabBarController
        
        setupNote()
        
        setupVCs()
        
        setSliderView()
    }
    
    deinit{
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
        
    }
}

extension CMDBSliderMenuController {
    
    func setupVCs(){
        
        navLeft                      = CustomNavigationControllerViewController(rootViewController: self.leftVc! , hideNavBar: true)
        
        navLeft.view.backgroundColor = UIColor.clearColor()

        leftVc?.nav                  = navLeft
        
        leftVc?.view.backgroundColor = UIColor.clearColor()
        
        self.mainVC                  = UIApplication.sharedApplication().keyWindow!.rootViewController as? CMDBTabBarController
        
        self.leftVc?.view.frame      = CGRect(x: -CMDB_Screen_Width, y: 0, width:CMDB_Screen_Width + 15, height: CMDB_Screen_Height)
        
        self.navLeft.view.frame      = (self.leftVc?.view.frame)!
        
    }

    
    func setupNote(){
//        NSNotificationCenter.defaultCenter().addObserver(self,selector: #selector(setupNavViewFrame), name: ShowDismissFrameNote, object: nil)
//        
//        
//        NSNotificationCenter.defaultCenter().addObserver(self,selector: #selector(isAllowLeftMenuOpen), name: ShowSliederDrawerNote, object: nil)
//        
//        NSNotificationCenter.defaultCenter().addObserver(self,selector: #selector(AllowLeftMenuOpen), name: AllowSliderDrawerNote, object: nil)
    }
    
    func setSliderView(){
        
        self.mainVC?.view.addSubview(self.navLeft.view)
        
        self.leftVc?.sliderView.showSliderBlock = {(
            UIView.animateWithDuration(kSliderMenuDuraton, animations: { [weak self] in
                self!.leftVc?.sliderView.superNav?.view.transform = CGAffineTransformMakeTranslation(CMDB_Screen_Width, 0)
                self!.leftVc?.sliderView.shadowView.alpha = 0.55
                })
            )}
    }
}


extension CMDBSliderMenuController{
    
    
}

extension CMDBSliderMenuController{
    
    func handlerDataBlock(content : AnyObject? ,index : AnyObject? ,isTap : AnyObject){
        
        if isTap as! Bool {
            
            let contentStr = content as! String
            
            if contentStr == "headIcon" {
                
                let userCenterVc = CMDBUserCenterViewController()
                
                userCenterVc.title = "我的主页"

                userCenterVc.hidesBottomBarWhenPushed = true;

                self.navigationController?.pushViewController(userCenterVc, animated: true)
                
            }
        }else{
            
            let userCenterVc   = CMDBUserCenterViewController()
            
            userCenterVc.title = "我的主页"
            
            userCenterVc.hidesBottomBarWhenPushed = true;
            
            self.navigationController?.pushViewController(userCenterVc, animated: true)

        }
    }
}


//MARK: handle geture
extension CMDBSliderMenuController {
    
    @objc func setupNavViewFrame(){

    }
    
    @objc func isAllowLeftMenuOpen(){
        
        
    }
    
    @objc func AllowLeftMenuOpen(){
        
    }
    
    @objc func dismissView(){
        
    }
    
    
    @objc func handlePanGesture(panGest : UIPanGestureRecognizer){
        
        
    }
    
}


